// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.commons.ui.swt.colorstyledtext.rules;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.util.Assert;

/**
 * An implementation of <code>IRule</code> capable of detecting words Word rules also allow for the association of
 * tokens with specific words. That is, not only can the rule be used to provide tokens for exact matches, but also for
 * the generalized notion of a word in the context in which it is used. A word rules uses a word detector to determine
 * what a word is.
 * 
 * /** Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class CasedWordRule implements IRule {

    protected static final int UNDEFINED = -1;

    /** The word detector used by this rule. */
    protected IWordDetector fDetector;

    /** The default token to be returned on success and if nothing else has been specified. */
    protected IToken fDefaultToken;

    /** The column constraint. */
    protected int fColumn = UNDEFINED;

    /** The table of predefined words and token for this rule. */
    protected Map<String, IToken> fWords = new HashMap<String, IToken>();

    protected boolean ignoreCase = false;

    private final StringBuffer fBuffer = new StringBuffer();

    /**
     * Creates a rule which, with the help of an word detector, will return the token associated with the detected word.
     * If no token has been associated, the scanner will be rolled back and an undefined token will be returned in order
     * to allow any subsequent rules to analyze the characters.
     * 
     * @param detector the word detector to be used by this rule, may not be <code>null</code>
     * 
     * @see #addWord
     */
    public CasedWordRule(IWordDetector detector) {
        this(detector, Token.UNDEFINED);
    }

    /**
     * Creates a rule which, with the help of an word detector, will return the token associated with the detected word.
     * If no token has been associated, the specified default token will be returned.
     * 
     * @param detector the word detector to be used by this rule, may not be <code>null</code>
     * @param defaultToken the default token to be returned on success if nothing else is specified, may not be
     * <code>null</code>
     * 
     * @see #addWord
     */
    public CasedWordRule(IWordDetector detector, IToken defaultToken) {
        Assert.isNotNull(detector);
        Assert.isNotNull(defaultToken);
        fDetector = detector;
        fDefaultToken = defaultToken;
    }

    public CasedWordRule(IWordDetector detector, IToken defaultToken, boolean ignoreCase) {
        this(detector, defaultToken);
        this.ignoreCase = ignoreCase;
    }

    /**
     * Adds a word and the token to be returned if it is detected.
     * 
     * @param word the word this rule will search for, may not be <code>null</code>
     * @param token the token to be returned if the word has been found, may not be <code>null</code>
     */
    public void addWord(String word, IToken token) {
        Assert.isNotNull(word);
        Assert.isNotNull(token);

        fWords.put(ignoreCase ? word.toLowerCase() : word, token);
    }

    /**
     * Sets a column constraint for this rule. If set, the rule's token will only be returned if the pattern is detected
     * starting at the specified column. If the column is smaller then 0, the column constraint is considered removed.
     * 
     * @param column the column in which the pattern starts
     */
    public void setColumnConstraint(int column) {
        if (column < 0) {
            column = UNDEFINED;
        }
        fColumn = column;
    }

    /*
     * @see IRule#evaluate
     */
    public IToken evaluate(ICharacterScanner scanner) {
        int c = scanner.read();
        if (fDetector.isWordStart((char) c)) {
            if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {

                fBuffer.setLength(0);
                do {
                    fBuffer.append((char) c);
                    c = scanner.read();
                } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
                scanner.unread();

                String lookup = fBuffer.toString();
                IToken token = fWords.get(ignoreCase ? lookup.toLowerCase() : lookup);
                if (token != null) {
                    return token;
                }

                if (fDefaultToken.isUndefined()) {
                    unreadBuffer(scanner);
                }

                return fDefaultToken;
            }
        }

        scanner.unread();
        return Token.UNDEFINED;
    }

    /**
     * Returns the characters in the buffer to the scanner.
     * 
     * @param scanner the scanner to be used
     */
    protected void unreadBuffer(ICharacterScanner scanner) {
        for (int i = fBuffer.length() - 1; i >= 0; i--) {
            scanner.unread();
        }
    }
}
